<?php
/**
 * Created by PhpStorm.
 * User: LiuJiaJun
 * Date: 2019/1/12
 * Time: 0:23
 */

namespace api\storage\service\inventory;

use api\storage\model\SeatModel;
use api\storage\model\ShelvesModel;
use api\storage\model\StorageModel;

/**
 * 站位盘点
 *
 * Class SeatInventoryService
 * @package api\storage\service\inventory
 */
abstract class BaseShelfInventoryService extends BaseInventoryService
{

    /**
     * 货架实现类的检查
     *
     * @param $code
     * @param $storageUuid
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function afterCheck($code, $storageUuid)
    {
        $seatModel = new SeatModel();
        //查找这个输入的站位是否存在
        $seat = $seatModel->findSeatCode($code, $storageUuid);
        if (empty($seat) || empty($seat->getData())) {
            return $this->resultTemplate(-11);
        }
        $len = strlen($code);
        //A01 ,A01L,A01L1 代表全货架，货架单面，货架单层三种方式货架盘点
        $len = $len >= 5 ? 5 : $len;
        return $this->resultTemplate(1, ['value' => substr($code, 0, $len)]);
    }


    protected function handleReels($reels)
    {
        $reelsInfo = [];
        $materiel = [];
        $materielType = [];
        foreach ($reels['reels'] as $k => $item) {
            $item['isFinish'] = false;
            $item['outCount'] = $item['count'];
            $reelsInfo[] = $item;
            $index = array_search($item['subMaterielId'], $materielType);
            if ($index !== false) {
                $materiel[$index]['index'][] = $k;
                $materiel[$index]['plate']++;
                $materiel[$index]['count'] += $item['count'];
                $materiel[$index]['seatId'][] = isset($item['seatId']) ? $item['seatId'] : [];
            } else {
                array_push($materielType, $item['subMaterielId']);
                $temp = [
                    'subMaterielId' => $item['subMaterielId'],
                    'index' => [$k],
                    'plate' => 1,
                    'count' => $item['count'],
                    'seatId' => isset($item['seatId']) ? [$item['seatId']] : [],
                    'confirm' => 0
                ];
                array_push($materiel, $temp);
            }
        }
        return $result = [
            'reels' => $reelsInfo,
            'header' => $materiel,
            'seatId' => $reels['seatId']
        ];

    }



}